/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.zhpan.bannerview;

import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.utils.Color;
import ohos.app.Context;

import com.zhpan.bannerview.base.BaseIndicatorComponent;
import com.zhpan.bannerview.base.MeasureResult;
import com.zhpan.bannerview.drawer.DrawerProxy;
import com.zhpan.bannerview.options.IndicatorOptions;
import com.zhpan.bannerview.options.Options;
import com.zhpan.bannerview.utils.AttrValue;

/**
 * IndicatorComponent
 *
 * @since 2021-03-24
 */
public class IndicatorComponent extends BaseIndicatorComponent implements Component.DrawTask {
    /**
     * TAG
     */
    public static final String TAG = IndicatorComponent.class.getSimpleName();

    private int width;
    private int height;

    private DrawerProxy mDrawerProxy;

    public IndicatorComponent(Context context) {
        this(context, null);
    }

    public IndicatorComponent(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public IndicatorComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        mDrawerProxy = new DrawerProxy(mIndicatorOptions);
        init(attrSet);
    }

    private void init(AttrSet attrSet) {
        int indicatorSlideMode = AttrValue.get(attrSet, "psi_slide_mode", Options.IndicatorMode.NORMAL);
        int indicatorStyle = AttrValue.get(attrSet, "psi_style", Options.IndicatorStyle.CIRCLE);
        Color checkedColor = AttrValue.get(attrSet, "psi_slider_checked_color", new Color(Color.getIntColor("#FF4C39")));
        Color normalColor = AttrValue.get(attrSet, "psi_slider_normal_color", new Color(Color.getIntColor("#935656")));
        int orientation = AttrValue.get(attrSet, "psi_orientation", Options.Orientation.INDICATOR_HORIZONTAL);
        float radius = AttrValue.get(attrSet, "psi_slider_radius", AttrHelper.vp2px(8, getContext()));

        mIndicatorOptions.setCheckedSliderColor(checkedColor);
        mIndicatorOptions.setNormalSliderColor(normalColor);
        mIndicatorOptions.setOrientation(orientation);
        mIndicatorOptions.setIndicatorStyle(indicatorStyle);
        mIndicatorOptions.setSlideMode(indicatorSlideMode);
        mIndicatorOptions.setSliderWidth(radius * 2,radius * 2);

        setEstimateSizeListener(new EstimateSizeListener() {

            @Override
            public boolean onEstimateSize(int widthSpec, int heightSpec) {
                MeasureResult measureResult = mDrawerProxy.onMeasure(widthSpec, heightSpec);

                width = measureResult.getMeasureWidth();
                height = measureResult.getMeasureHeight();

                setEstimatedSize(width, height);
                Log.debug(TAG, "onEstimateSize width=%{public}d, height=%{public}d", IndicatorComponent.this.width, height);
                return false;
            }
        });
        addDrawTask(this);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        setComponentSize(width, height);
        rotateCanvas(canvas);
        mDrawerProxy.onDraw(component, canvas);
    }

    @Override
    public void setIndicatorOptions(IndicatorOptions options) {
        super.setIndicatorOptions(options);
        mDrawerProxy.setOptions(options);
    }

    @Override
    public void notifyDataChanged() {
        mDrawerProxy = new DrawerProxy(mIndicatorOptions);
        super.notifyDataChanged();
    }

    private void rotateCanvas(Canvas canvas) {
        int orientation = mIndicatorOptions.getOrientation();
        int width1 = getWidth();
        if (orientation == Options.Orientation.INDICATOR_VERTICAL) {
            canvas.rotate(90f, getWidth() / 2f, width1 / 2f);
        } else if (orientation == Options.Orientation.INDICATOR_RTL) {
            canvas.rotate(180f, width1 / 2f, getHeight() / 2f);
        }
    }

    /**
     * setOrientation
     *
     * @param orientation 參數
     */
    public void setOrientation(int orientation) {
        mIndicatorOptions.setOrientation(orientation);
    }

}

